<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="任务名称" prop="nodeName">
        <el-input
          v-model="queryParams.nodeName"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流程状态" prop="flowStatus">
        <el-select
          v-model="queryParams.nodeType"
          placeholder="请选择流程状态"
          clearable
        >
          <el-option
            v-for="dict in flow_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="抄送人" prop="flowName">
        <el-input
          v-model="queryParams.flowName"
          placeholder="请输入任抄送人"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="instanceList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" fixed />
      <el-table-column label="序号" width="55" align="center">
        <template #default="{ $index }">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column
        label="流程实例id"
        width="100"
        align="center"
        prop="id"
        show-overflow-tooltip
      />
      <el-table-column
        label="流程名称"
        align="center"
        prop="flowName"
        show-overflow-tooltip
      />
      <el-table-column
        label="任务名称"
        align="center"
        prop="nodeName"
        show-overflow-tooltip
      />
      <el-table-column
        label="抄送人"
        align="center"
        prop="approver"
        show-overflow-tooltip
      />
      <el-table-column
        label="流程状态"
        align="center"
        prop="flowStatus"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <dict-tag :options="flow_status" :value="row.flowStatus"/>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="160"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <span>{{ parseTime(row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="240"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template #default="{ row }">
          <el-button
            size="small"
            type="text"
            @click="toFlowImage(row.id)"
          >流程图</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <component
      :is="approve"
      v-model="businessId"
      :taskId="taskId"
      @refresh="getList"
    />

    <el-dialog
      title="流程图"
      width="70%"
      v-model="flowChart"
      append-to-body
    >
      <WarmChart :insId="insId" />
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { Search, Refresh } from '@element-plus/icons-vue'
import * as api from '@/api/flow/execute'
import WarmChart from '@/views/flow/definition/warm_chart.vue'
import { parseTime } from '@/utils/ruoyi'
import { useDict } from '@/utils/dict'

// 字典数据
const { flow_status } = useDict(['flow_status'])

// 状态管理
const loading = ref(true)
const insId = ref(null)
const flowChart = ref(false)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const showSearch = ref(true)
const total = ref(0)
const instanceList = ref([])
const approve = ref(null)
const taskId = ref('')
const businessId = ref('')

// 查询参数
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  nodeName: null,
  flowStatus: null,
  approver: null,
  flowName: null,
  createTime: null,
})

// 查询流程实例列表
const getList = async () => {
  loading.value = true
  try {
    const response = await api.copyPage(queryParams.value)
    instanceList.value = response.rows
    total.value = response.total
  } finally {
    loading.value = false
  }
}

// 搜索按钮操作
const handleQuery = () => {
  queryParams.value.pageNum = 1
  getList()
}

// 重置按钮操作
const resetQuery = () => {
  queryForm.value.resetFields()
  handleQuery()
}

// 多选框选中数据
const handleSelectionChange = (selection) => {
  ids.value = selection.map(item => item.id)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

// 办理按钮操作
const handle = (row) => {
  taskId.value = row.id
  businessId.value = row.businessId
  if (row.formCustom === 'N' && row.formPath) {
    approve.value = defineAsyncComponent(() => import(`@/views/${row.formPath}`))
  }
}

// 查看流程图
const toFlowImage = (instanceId) => {
  insId.value = instanceId
  flowChart.value = true
}

// 初始化加载数据
onMounted(() => {
  getList()
})
</script>
